** Use data from all studies
set more off
cd "${dropbox}/good guys/paper/data_code"


** Study 1 **
tempfile study1
use gbc_s1, clear
set scheme s1color

keep session treatmentnumber period subject bidrag type nytype group gruppetype pers
gen nogroup= group if session==20100 
replace nogroup = group+6 if session==20200 
replace nogroup = group+11 if session==20300 
replace nogroup = group+17 if session==20400
replace nogroup = group+23 if session==20500
gen gid=session +group
forvalues g=1/29 {
gen g`g' = 1 if nogroup==`g'
}

sort pers period
replace type=type[_n+1] if treatmentnumber==1
replace type=type[_n-1] if treatmentnumber==2
replace gruppetype= gruppetype[_n+1] if treatmentnumber==1
replace nytype=nytype[_n+1] if treatmentnumber==1

rename bidrag Bidrag
replace Bidrag=100*Bidrag/60
keep if gruppetype==1 // Keep homogenous groups
collapse (mean) Bidrag (sem) se_bidrag=Bidrag, by(period type)

replace period=-1 if period==0
reshape wide Bidrag se, i(period) j(type)

*drop if period>10

*replace period=11+.2*(period-11) if period>10

gen study=1
save `study1'


*April study 4
tempfile s4 
use gbc_s4, clear
replace Period=Period+10 if treatment==4
gen period=Period if treatment>2
replace period=-1 if treatment==1
replace period=0  if treatment==2

sort id period
gen type=NyType
replace type=type[_n+1] if treatment==2
replace type=type[_n+1] if treatment==1

* Keep if homogenous group
drop if GruppeType==0

replace Bidrag=Bidrag*100/60
collapse (mean) Bidrag (sem) se_bidrag=Bidrag, by(type period)
reshape wide Bidrag se, i(period) j(type)
gen study=4
save `s4'

*October
tempfile oct 
use gbc_s3, clear
save `oct'

*March
use gbc_s2, clear
replace id=id+1000
append using `oct'
*drop if treatment==4 //Not comparable across exp

*do "estimation/code_types.do"
sort id treatment Period
bysort id: egen type=mean(NyType) if !(study==2 & treatment==4)
replace type=NyType if (study==2 & treatment==4)

** Type X=2 (blue), Z=1 (red)
lab def group_types 1 "Red" 2 "Blue"
gen red=type==1
lab val type group_types


replace Period=Period+10 if treatment==4
gen period=Period if treatment>2
replace period=-1 if treatment==1
replace period=0  if treatment==2
*keep if treatment==3

* Keep if homogenous group
drop if GruppeType==0

replace Bidrag=Bidrag*100/60
collapse (mean) Bidrag (sem) se_bidrag=Bidrag, by(type period study)
reshape wide Bidrag se, i(period study) j(type)

append using `study1'
append using `s4'


local line_opt mc(red blue) c(l l) lc(red blue) lp(solid dash) msize(small small)



tw scatter Bidrag1 Bidrag2 period if inrange(period,-1,0) & study==1, mc(red blue) ms(O Oh) || ///
	scatter Bidrag1 Bidrag2 period if inrange(period,-1,0) & study==2, mc(red blue) ms(D Dh) || ///
	scatter Bidrag1 Bidrag2 period if !inrange(period,1,10) & study==3, mc(red blue) ms(T Th) || ///
	scatter Bidrag1 Bidrag2 period if !inrange(period,1,10) & study==3, mc(red blue) ms(S Sh) || ///
	scatter Bidrag1 Bidrag2 period if inrange(period,1,10) & study==1, `line_opt'  ms(O Oh) || ///
	scatter Bidrag1 Bidrag2 period if inrange(period,11,30) & study==1, `line_opt'  ms(O Oh) || ///
	scatter Bidrag1 Bidrag2 period if inrange(period,1,10) & study==2, `line_opt' ms(D Dh) || ///
	scatter Bidrag1 Bidrag2 period if inrange(period,11,15) & study==2, `line_opt' ms(D Dh) || ///
	scatter Bidrag1 Bidrag2 period if inrange(period,1,10) & study==3, `line_opt' ms(T Th)  || ///	
	scatter Bidrag1 Bidrag2 period if inrange(period,1,10) & study==4, `line_opt'  ms(S Sh) || ///
	scatter Bidrag1 Bidrag2 period if inrange(period,11,30) & study==4, `line_opt'  ms(S Sh) ///
	ylab(0(10)100) xlab(1 5 10 15 20 25 30) /// * xsc(r(-1.3 10.2)) ///
	xline(0.5, lc(black) lp(dash))  ///
	xline(-0.5, lc(black) lp(dash))  ///
	xline(10.5, lc(black) lp(dash))  ///
	xtitle(Period) ytitle("Contribution (% of endowment)") ///
	legend(order(1 3 5 7) label(1 "Study 1") label(3 "Study 2") label(5 " Study 3") label(7 " Study 4") row(1))


